package personal.wang.juc;

import java.io.IOException;
import java.util.concurrent.*;
import java.util.function.BiConsumer;

public class ProducerAndConsumer {
    public static void main(String[] args) throws IOException {
//        while(true){
//            CompletableFuture.supplyAsync(() -> {
//                return "aa";
//            }, Executors.newSingleThreadExecutor())
//                            .thenAcceptAsync(x -> {
//                System.out.println("consumer: " + x);
//            }, Executors.newSingleThreadExecutor());
//            try {
//                Thread.sleep(1000L);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//        }
        method();

    }
    public static void method(){
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(10);
        ExecutorService producer = Executors.newFixedThreadPool(2);
        ExecutorService consumner = Executors.newFixedThreadPool(2);
        producer.execute(()->{
            while(true){
                try {
                    queue.put("aaa");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        consumner.execute(()->{
            while(true){
                try {
                    System.out.println(queue.take());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        try {
            System.in.read();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}
